﻿using System;
using TrinityAPI.Base.Models;

namespace TrinityAPI.Base.Tools
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogHelper
    {
        #region Error
        /// <summary>
        /// Adds an error log
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        /// <param name="loggerName"></param>
        public static void Error(object message, Exception exception, string loggerName = null)
        {
            string service = APIContext.Service;
            if (string.IsNullOrWhiteSpace(service))
                Sys.Log.LogHelper.Error(message, exception, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
            else
                Sys.Log.LogHelper.Error(new LogRouter() { Service = service, Message = message }, exception, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
        }
        #endregion

        #region Warn
        /// <summary>
        /// Adds an warn log
        /// </summary>
        /// <param name="message"></param>
        /// <param name="loggerName"></param>
        public static void Warn(object message, string loggerName = null)
        {
            string service = APIContext.Service;
            if (string.IsNullOrWhiteSpace(service))
                Sys.Log.LogHelper.Warn(message, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
            else
                Sys.Log.LogHelper.Warn(new LogRouter() { Service = service, Message = message }, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
        }

        /// <summary>
        /// Adds an warn log with exception
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        /// <param name="loggerName"></param>
        public static void Warn(object message, Exception exception, string loggerName = null)
        {
            string service = APIContext.Service;
            if (string.IsNullOrWhiteSpace(service))
                Sys.Log.LogHelper.Warn(message, exception, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
            else
                Sys.Log.LogHelper.Warn(new LogRouter() { Service = service, Message = message }, exception, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
        }

        #endregion

        #region Info
        /// <summary>
        /// Traces a message
        /// </summary>
        /// <param name="message"></param>
        /// <param name="loggerName"></param>
        public static void Info(object message, string loggerName = null)
        {
            string service = APIContext.Service;
            if (string.IsNullOrWhiteSpace(service))
                Sys.Log.LogHelper.Info(message, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
            else
                Sys.Log.LogHelper.Info(new LogRouter() { Service = service, Message = message }, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
        }
        #endregion

        #region Debug
        /// <summary>
        /// Debugs if tracing is enabled.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="loggerName"></param>
        public static void Debug(object message, string loggerName = null)
        {
            string service = APIContext.Service;
            if (string.IsNullOrWhiteSpace(service))
                Sys.Log.LogHelper.Debug(message, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
            else
                Sys.Log.LogHelper.Debug(new LogRouter() { Service = service, Message = message }, loggerName, APIContext.LogRepositoryName, APIContext.LogConfigFile);
        }
        #endregion
    }
}
